package cn.iocoder.yudao.module.book.controller.admin.bookshelf;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.book.service.admin.bookshelf.BookShelfService;
import cn.iocoder.yudao.module.book.service.admin.bookshelfcatalogue.BookShelfCatalogueService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.book.controller.admin.bookshelf.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.bookshelf.BookShelfDO;

@Tag(name = "管理后台 - 绘本集")
@RestController
@RequestMapping("/book/shelf")
@Validated
public class BookShelfController {

    @Resource
    private BookShelfService shelfService;

    @Resource
    private BookShelfCatalogueService bookShelfCatalogueService;

    @PostMapping("/create")
    @Operation(summary = "创建绘本集")
    @PreAuthorize("@ss.hasPermission('book:shelf:create')")
    public CommonResult<Long> createShelf(@Valid @RequestBody BookShelfSaveReqVO createReqVO) {
        return success(shelfService.createShelf(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新绘本集")
    @PreAuthorize("@ss.hasPermission('book:shelf:update')")
    public CommonResult<Boolean> updateShelf(@Valid @RequestBody BookShelfSaveReqVO updateReqVO) {
        shelfService.updateShelf(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除绘本集")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('book:shelf:delete')")
    public CommonResult<Boolean> deleteShelf(@RequestParam("id") Long id) {
        shelfService.deleteShelf(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除绘本集")
                @PreAuthorize("@ss.hasPermission('book:shelf:delete')")
    public CommonResult<Boolean> deleteShelfList(@RequestParam("ids") List<Long> ids) {
        shelfService.deleteShelfListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得绘本集")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('book:shelf:query')")
    public CommonResult<BookShelfRespVO> getShelf(@RequestParam("id") Long id) {
        BookShelfDO shelf = shelfService.getShelf(id);
        return success(BeanUtils.toBean(shelf, BookShelfRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得绘本集分页")
    @PreAuthorize("@ss.hasPermission('book:shelf:query')")
    public CommonResult<PageResult<BookShelfRespVO>> getShelfPage(@Valid BookShelfPageReqVO pageReqVO) {
        PageResult<BookShelfDO> pageResult = shelfService.getShelfPage(pageReqVO);
        PageResult<BookShelfRespVO> voPageResult = BeanUtils.toBean(pageResult, BookShelfRespVO.class);

        List<BookShelfRespVO> voPageResultList = voPageResult.getList();
        if(CollUtil.isNotEmpty(voPageResultList)){
            voPageResultList.forEach(vo -> {
                vo.setCatalogueName(bookShelfCatalogueService.getCatalogueNameById(vo.getCatalogueId()));
            });
            voPageResult.setList(voPageResultList);
        }
        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出绘本集 Excel")
    @PreAuthorize("@ss.hasPermission('book:shelf:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportShelfExcel(@Valid BookShelfPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BookShelfDO> list = shelfService.getShelfPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "绘本集.xls", "数据", BookShelfRespVO.class,
                        BeanUtils.toBean(list, BookShelfRespVO.class));
    }

}